package com.handpay.nocardpay.baselibrary.setting.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.widget.SwitchButton;
import com.handpay.nocardpay.baselibrary.setting.presenter.GesturePresenterImpl;
import com.handpay.nocardpay.baselibrary.setting.view.IGesturePWDGetView;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.PreferenceManager;
import com.handpay.nocardpay.baselibrary.widget.CustomTextView;

import java.util.HashMap;

/**
 * Created by lyang on 2017/10/16.
 */

public class SettingGesturePWDFragment extends BaseFragment implements View.OnClickListener, IGesturePWDGetView {
    private SwitchButton use_gesture_pwd_switch_button;
    private CustomTextView modify_gesture_pwd_ctv;
    private CustomTextView forget_gesture_pwd_ctv;
    public static int REQUEST_MODIFY_GESTURE_PWD = 0;   //修改手势密码
    public static int REQUEST_FORGET_GESTURE_PWD = 1;   //忘记手势密码
    public static int REQUEST_NEW_GESTURE_PWD = 2;      //新增手势密码
    public static int RESULT_VERIFY_GESTURE_PWD_SUCCESS = 3;//修改手势密码验证成功
    public static int RESULT_VERIFY_GESTURE_PWD_ERROR = 4;
    public static int RESULT_VERIFY_OTP_SUCCESS = 5;//忘记手势密码验证成功

    public static SettingGesturePWDFragment newInstance() {
        Bundle args = new Bundle();
        SettingGesturePWDFragment fragment = new SettingGesturePWDFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_setting_gresture_pwd;
    }

    @Override
    public void initView(View view) {
        Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true);
        use_gesture_pwd_switch_button = (SwitchButton) view.findViewById(R.id.use_gesture_pwd_switch_button);
        modify_gesture_pwd_ctv = (CustomTextView) view.findViewById(R.id.modify_gesture_pwd_ctv);
        forget_gesture_pwd_ctv = (CustomTextView) view.findViewById(R.id.forget_gesture_pwd_ctv);
        String gesture_pwd = ClientEngine.getInstance().getRMSData(BaseProtocolKey.KEY_GESTURE_PWD);
        boolean bGestureSwitchStatus = PreferenceManager.getInstance(_mActivity).getBoolean(BaseProtocolKey.KEY_GESTURE_PWD_SWITCH_STATUS, true);//手势密码开关
        if (!TextUtils.isEmpty(gesture_pwd) && bGestureSwitchStatus) {
            setGesturePwdStatus(true);
        } else {
            setGesturePwdStatus(false);
        }
    }

    @Override
    public void initListener() {
        modify_gesture_pwd_ctv.setOnClickListener(this);
        forget_gesture_pwd_ctv.setOnClickListener(this);
        use_gesture_pwd_switch_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!TextUtils.isEmpty(ClientEngine.getInstance().getRMSData(BaseProtocolKey.KEY_GESTURE_PWD))) {
                    //已经有手势密码
                    if (isChecked) {
                        PreferenceManager.getInstance(_mActivity).putBoolean(BaseProtocolKey.KEY_GESTURE_PWD_SWITCH_STATUS, true);
                        setGesturePwdStatus(true);
                    } else {
                        PreferenceManager.getInstance(_mActivity).putBoolean(BaseProtocolKey.KEY_GESTURE_PWD_SWITCH_STATUS, false);
                        setGesturePwdStatus(false);
                    }

                } else {
                    //手势密码空
                    if (isChecked) {
                        startForResult(GestureLockSettingFragment.newInstance(),REQUEST_NEW_GESTURE_PWD);
                    }
                }
            }
        });
    }

    private void setGesturePwdStatus(boolean isOpen) {
        if (isOpen) {
            use_gesture_pwd_switch_button.setChecked(isOpen);
            modify_gesture_pwd_ctv.setVisibility(View.VISIBLE);
            forget_gesture_pwd_ctv.setVisibility(View.VISIBLE);
        } else {
            use_gesture_pwd_switch_button.setChecked(isOpen);
            modify_gesture_pwd_ctv.setVisibility(View.GONE);
            forget_gesture_pwd_ctv.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        GesturePresenterImpl gesturePresenter = new GesturePresenterImpl(_mActivity);
        gesturePresenter.setGesturePwdGetView(this);
        HashMap<String, Object> params = new HashMap<String, Object>();
        String phone = AccountUtils.getAccountPhone(_mActivity);
        params.put(BaseProtocolKey.MOBILE, SecureManager.getInstance().des(phone, 1, null));
        gesturePresenter.getLoginUserGesturePwd(params, true);
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.setting_gesture_pwd);
    }

    @Override
    public void onClick(View v) {
        if (R.id.modify_gesture_pwd_ctv == v.getId()) {
            startForResult(GestureLockVerifyFragment.newInstance(), REQUEST_MODIFY_GESTURE_PWD);
        } else if (R.id.forget_gesture_pwd_ctv == v.getId()) {
            startForResult(ForgetGesturePWDFragment.newInstance(), REQUEST_FORGET_GESTURE_PWD);
        }
    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_MODIFY_GESTURE_PWD) {
            if (resultCode == RESULT_VERIFY_GESTURE_PWD_SUCCESS) {
                start(GestureLockSettingFragment.newInstance());
            }
        } else if (requestCode == REQUEST_FORGET_GESTURE_PWD) {
            if (resultCode == RESULT_VERIFY_OTP_SUCCESS) {
                start(GestureLockSettingFragment.newInstance());
            }
        } else if (requestCode == REQUEST_NEW_GESTURE_PWD) {
            boolean bGesturePwdSwitch = PreferenceManager.getInstance(_mActivity).getBoolean(BaseProtocolKey.KEY_GESTURE_PWD_SWITCH_STATUS, false);
            setGesturePwdStatus(bGesturePwdSwitch);
        }
    }

    @Override
    public void onGesturePWDSuccess() {
        String gesture_pwd = ClientEngine.getInstance().getRMSData(BaseProtocolKey.KEY_GESTURE_PWD);
        boolean bGestureSwitchStatus = PreferenceManager.getInstance(_mActivity).getBoolean(BaseProtocolKey.KEY_GESTURE_PWD_SWITCH_STATUS, true);//手势密码开关
        if (!TextUtils.isEmpty(gesture_pwd) && bGestureSwitchStatus) {
            setGesturePwdStatus(true);
        } else {
            setGesturePwdStatus(false);
        }
    }
}
